package com.example.rqweb.Volly;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.rqweb.R;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
public class volly extends AppCompatActivity {

        private Button get;
        private Button post;
        private Button json;
       /* private Button imagerequest;
        private  Button imageload;
        private  Button netWorkImageView;

        */
       String url="https://www.baidu.com";

        private ImageView iv;
        private NetworkImageView network;
        private TextView tv_volley_result;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.post);
            initview();
            initListener();
            /*imagerequest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });*/


           /* imageload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

            netWorkImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });*/
        }
        public void initview()
        {

            get=findViewById(R.id.get);
            post=findViewById(R.id.post);
            json=findViewById(R.id.json);
            /*imagerequest=findViewById(R.id.ImageRquest);
            imageload=findViewById(R.id.ImageLoader);
            netWorkImageView=findViewById(R.id.NetWorkImageView);*/
            iv=findViewById(R.id.iv_volley);
            network=findViewById(R.id.NetWork);
            tv_volley_result=findViewById(R.id.volley_resul);


        }

        public void initListener()
        {
                  get.setOnClickListener(new View.OnClickListener() {

                          @Override
                       public void onClick(View view) {
                    //创建一个请求队列
                              RequestQueue requestQueue=Volley.newRequestQueue(volly.this);
                      //创建一个请求
                     String url="https://www.baidu.com";

                      StringRequest stringRequest=new StringRequest(url, new Response.Listener<String>() {


                        @Override
                        public void onResponse(String response) {
                            tv_volley_result.setText(response);
                        }
                    }, new Response.ErrorListener() {//发生异常之后的监听回调
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            tv_volley_result.setText("加载错误"+error);
                        }
                    });

                    //将创建的请求添加到请求队列当中
                    requestQueue.add(stringRequest);
                }
            });



            post.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 1 创建一个请求队列
                    RequestQueue requestQueue = Volley.newRequestQueue(volly.this);

                    // 2 创建一个post请求
                    String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";

                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            tv_volley_result.setText(s);
                        }
                    },
                            new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            tv_volley_result.setText("请求失败" + volleyError);
                        }
                    })


                    {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {

                            Map<String, String> map = new HashMap<String, String>();
//                        map.put("value1","param1");

                            return map;
                        }
                    };

                    // 3 将post请求添加到队列中
                    requestQueue.add(stringRequest);




                }
            });


            json.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                      // 1 创建一个请求队列
                    RequestQueue requestQueue = Volley.newRequestQueue(volly.this);

                    // 2 创建一个请求
                   // String url = "http://gank.io/api/xiandu/category/wow";
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            tv_volley_result.setText(jsonObject.toString());
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            tv_volley_result.setText("请求失败" + volleyError);
                        }
                    });

                    // 3 将创建的请求添加到请求队列中
                    requestQueue.add(jsonObjectRequest);



                }
            });



        }
    }